﻿// -----------------------------------------------------------------------
// <copyright file="UpdateUserCommandHandler.cs">
// Copyright (c) 2013-2014 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.CommandHandlers.Users
{
	using System.Diagnostics.Contracts;
	using System.Linq;
	using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.Database.Dto;
	using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.Model.Commands.Users;

	internal class UpdateUserCommandHandler : ICommandHandlerSync<UpdateUserCommand>
	{
		private readonly IRepository _repository;
		private readonly IDataSource _dataSource;

		public UpdateUserCommandHandler(IRepository repository, IDataSource dataSource)
		{
			Contract.Requires(repository != null);
			Contract.Requires(dataSource != null);

			this._repository = repository;
			this._dataSource = dataSource;
		}

		public void Execute(UpdateUserCommand command)
		{
			var userInfo = this._dataSource
				.Set<UserDto>()
				.FirstOrDefault(u => u.Id == command.Id);

			if (userInfo != null) {
				userInfo.Name = command.Name;
				userInfo.Email = command.Email;
				userInfo.IsDisabled = command.IsDisabled;

				this._repository.Update(userInfo);
			}
		}
	}
}
